package ${packageName}.service.impl;

import java.util.List;
import java.util.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.lam.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import ${packageName}.mapper.${ClassName}Mapper;
import ${packageName}.entity.${ClassName};
import ${packageName}.param.${ClassName}Param;
import ${packageName}.service.${ClassName}Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
<#if hasFuncImport> 
import com.lam.common.components.easyexcel.imp.model.AbstractImportBody;
import com.lam.common.components.easyexcel.imp.model.ImportEntry;
import com.lam.common.components.easyexcel.imp.model.ImportWrapper;
import com.lam.common.components.easyexcel.imp.service.AbstractImportService;
import ${packageName}.entity.imp.${ClassName}Imp;
	<#if hasFuncUnique> 
import com.lam.common.validate.model.UniqueDesc;
import com.lam.common.validate.ValidateUtil;
import com.lam.common.validate.model.ValidOperType;
		<#if moduleName == 'system'> 
import com.lam.common.constant.${ModuleName}Constant;
		<#else> 
import com.lam.${moduleName}.common.constant.${ModuleName}Constant;
		</#if> 
import java.text.MessageFormat;
	</#if> 
</#if> 

/**
 * ${functionName}Service业务层处理
 * 
 * @author ${author}
 * @date ${datetime}
 */
@Service
public class ${ClassName}ServiceImpl <#if hasFuncImport>extends AbstractImportService<${ClassName}Imp, ${ClassName}></#if> implements ${ClassName}Service {

    @Autowired
    private ${ClassName}Mapper ${className}Mapper;

    /**
     * 查询${functionName}
     * 
     * @param ${pkColumn.javaField} ID
     * @return 单个对象
     */
    @Override
    @Transactional(readOnly = true)
    public ${ClassName} selectById(String ${pkColumn.javaField}) {

        return ${className}Mapper.selectById(${pkColumn.javaField});
    }

    /**
     * 查询${functionName}列表
     * 
     * @param param
     * @return 
     */
    @Override
    @Transactional(readOnly = true)
    public List<${ClassName}> selectList(${ClassName}Param param) {
    
        QueryWrapper<${ClassName}> queryWrapper = new QueryWrapper<>();
<#if hasStrLikeQueryField> 

        //关键字查询
        if(StringUtils.isNotBlank(param.getKeywords())) {
            queryWrapper.and(wr->{
    <#list columns as column>
    	<#if column.query && column.strDataType && column.queryType == "LIKE"> 
                wr.or().like("${column.columnName}", param.getKeywords());
    	</#if> 
    </#list> 
            });
        }
</#if> 
        
<#list columns as column>
	<#if column.query> 
<#--时间类型的判断-开始 -->
		<#if column.columnType == "date" || column.columnType == "datetime"> 
			<#if column.queryType == "BETWEEN"> 
        queryWrapper.ge(Objects.nonNull(param.get${column.attrName}Begin()), "${column.columnName}", param.get${column.attrName}Begin());
        queryWrapper.le(Objects.nonNull(param.get${column.attrName}End()), "${column.columnName}", param.get${column.attrName}End());
        
        	<#else> 
        queryWrapper.eq(Objects.nonNull(param.get${column.attrName}()), "${column.columnName}", param.get${column.attrName}());
        	</#if> 
<#--时间类型的判断-结束 -->
<#--字符串类型判断-开始 -->
		<#elseif column.strDataType>
			<#if column.queryType == "LIKE"> 
        queryWrapper.like(StringUtils.isNotBlank(param.get${column.attrName}Key()), "${column.columnName}", param.get${column.attrName}Key());
        	<#elseif column.queryType == "NE"> 
        queryWrapper.ne(StringUtils.isNotBlank(param.get${column.attrName}()), "${column.columnName}", param.get${column.attrName}());
        	</#if> 
        queryWrapper.eq(StringUtils.isNotBlank(param.get${column.attrName}()), "${column.columnName}", param.get${column.attrName}());
<#--字符串类型判断-结束 -->
<#--其他串类型判断-开始 -->
		<#else> 
        	<#if column.queryType == "GT"> 
        queryWrapper.gt(Objects.nonNull(param.get${column.attrName}()), "${column.columnName}", param.get${column.attrName}());
        	<#elseif column.queryType == "GTE"> 
        queryWrapper.ge(Objects.nonNull(param.get${column.attrName}()), "${column.columnName}", param.get${column.attrName}());
        	<#elseif column.queryType == "LT"> 
        queryWrapper.lt(Objects.nonNull(param.get${column.attrName}()), "${column.columnName}", param.get${column.attrName}());
        	<#elseif column.queryType == "LTE"> 
        queryWrapper.le(Objects.nonNull(param.get${column.attrName}()), "${column.columnName}", param.get${column.attrName}());
        	<#elseif column.queryType == "BETWEEN"> 
        queryWrapper.ge(Objects.nonNull(param.get${column.attrName}Begin()), "${column.columnName}", param.get${column.attrName}Begin());
        queryWrapper.le(Objects.nonNull(param.get${column.attrName}End()), "${column.columnName}", param.get${column.attrName}End());
        
        	<#elseif column.queryType == "NE"> 
        queryWrapper.ne(Objects.nonNull(param.get${column.attrName}()), "${column.columnName}", param.get${column.attrName}());
        	<#else> 
        queryWrapper.eq(Objects.nonNull(param.get${column.attrName}()), "${column.columnName}", param.get${column.attrName}());
        	</#if> 
<#--其他类型判断-结束 -->
		</#if> 
	</#if> 
</#list> 
<#if table.simpleTree> 
        queryWrapper.ne(StringUtils.isNotBlank(param.getNeId()), "id", param.getNeId());
</#if> 

        return ${className}Mapper.selectList(queryWrapper);
    }

    /**
     * 新增${functionName}
     * 
     * @param ${className} 
     * @return 
     */
    @Override
    @Transactional
    public int insert(${ClassName} ${className}) {
    
        return ${className}Mapper.insert(${className});
    }

    /**
     * 修改${functionName}
     * 
     * @param ${className} 
     * @return 
     */
    @Override
    @Transactional
    public int update(${ClassName} ${className}) {
    
        return ${className}Mapper.updateById(${className});
    }

    /**
     * 删除${functionName}信息
     * 
     * @param idStr ID，多个用逗号分隔
     * @return 
     */
    @Override
    @Transactional
    public int deleteById(String idStr) {
    
        if(StringUtils.isBlank(idStr)) {
            return 0;
        }
        return ${className}Mapper.deleteBatchIds(StringUtils.str2List(idStr));
    }
    
<#if hasFuncImport> 
    /**
     * 导入${functionName}数据
     * @param wrapper ${functionName}数据包装对象
     */
    @Override
    @Transactional
    public void importData(ImportWrapper<${ClassName}Imp, ${ClassName}> wrapper) {
        
    <#if hasFuncUnique> 
        UniqueDesc uniqueDesc = ValidateUtil.selectUniqueDesc(${ModuleName}Constant.${uniqueCodeConstant});
        importData(wrapper, new AbstractImportBody<${ClassName}Imp, ${ClassName}>() {
            @Override
            public ${ClassName} selectRecord(ImportEntry<${ClassName}Imp, ${ClassName}> entry) {
                return ValidateUtil.selectUniqueData(entry.getData(), ValidOperType.ADD, uniqueDesc, ${ClassName}.class);
            }
            @Override
			public int insertData(${ClassName} data) {
				return insert(data);
			}
			@Override
			public int updateData(${ClassName} data) {
				return update(data);
			}
            @Override
            public String notUniqueMsg(ImportEntry<${ClassName}Imp, ${ClassName}> entry) {
                return MessageFormat.format("${uniqueFieldName}【{0}】已存在", entry.getData().get${uniqueFieldFirstU}());
            }
        });
    <#else> 
        importData(wrapper, new AbstractImportBody<${ClassName}Imp, ${ClassName}>() {
            @Override
			public int insertData(${ClassName} data) {
				return insert(data);
			}
        });
    </#if> 
    }
</#if> 
    
}
